package com.dragon.sdgp.controller;

import com.dragon.sdgp.common.R;
import com.dragon.sdgp.util.CreateUtil;
import com.dragon.sdgp.util.EmailUtil;
import com.dragon.sdgp.util.StringUtil;
import com.dragon.sdgp.util.VerificationCodeUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR : Dragon_Yang
 * @File : CommonController
 * @DESCRIPTION :
 * @DATE : 2023/3/12 18:11
 */
@RestController
@RequestMapping("/api/common")
public class CommonController {

    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private VerificationCodeUtil verificationCodeUtil;

    @Autowired
    private CreateUtil createUtil;

    @GetMapping("/vCode")
    public R getVerificationCodeOnEmail(@Param("to") String to){
        if(StringUtil.isEmpty(to)){
            return R.success(400, "error");
        }
        String vCode = verificationCodeUtil.getRandomCode(to);
        emailUtil.send(to, "SDGP注册验证码", "验证码：" + vCode + "   【验证码 5 分钟内有效】");
        return R.success(200, "success");
    }

    @GetMapping("/isLogin")
    public R isLogin(){
        return R.success(200, null);
    }

    @PostMapping("/create")
    public R create(@Param("count") Integer count,
                    @RequestBody List<Map<String, String>> columns){
        if(count > 999) return R.fail(400, "超出限制！");
        return R.success(200, createUtil.createDataList(count, columns));
    }
}
